package tech.jabari.config;

import lombok.val;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import tech.jabari.utils.DataSourceUtil;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * @author Jabari Lu
 * @title: AppConfig
 * @data 2020/8/21 9:46
 */
@Configuration
@EnableAspectJAutoProxy // 开启切面的自动代理功能
@EnableTransactionManagement //开启基于注解的事务管理功能
public class AppConfig {

    /**
     * 创建DataSource对象，相当于在xml文件中配置：<br/>
     * 使用静态工厂方式，产生datasource对象:<br/>
     *     <bean id="dataSource" class="tech.jabari.utils.DataSourceUtil"
     *         factory-method="getDataSource"></bean>
     * </bean>
     * @return
     */
    @Bean
    public DataSource dataSource() {
        return DataSourceUtil.getDataSource();
    }


    /**
     * 创建jdbcTemplate对象。<br/>
     * <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
     *         <property name="dataSource" ref="dataSource"/>
     *     </bean>
     * @return
     */
    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }


    /**
     * 注册事务管理器
     */
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager()
            throws Exception{
        return new DataSourceTransactionManager(dataSource());
    }


    /**
     * 注入MyBatis的SqlSessionFactoryBean。<br/>
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath*:mapper/UserMapper.xml"/>
     </bean>
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 设置数据源。
        sqlSessionFactoryBean.setDataSource(dataSource());
        // 设置Mapper映射文件的路径。
        PathMatchingResourcePatternResolver classPathResource = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(classPathResource.getResources("classpath*:mapper/*Mapper.xml"));
        return sqlSessionFactoryBean;
    }







}
